package com.fit.pianosheetshop.pages;

import java.util.Collection;

import com.fit.pianosheetshop.interfaces.*;
import com.fit.pianosheetshop.beans.*;
import com.fit.pianosheetshop.service.*;

import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.*;





public class EntryBorrow {
	
	// Stringovi u kojima se cuva unique id informacija za izabranog
	// citaoca, bibliotekara, knjigu i iznajmljivanje
	@Persist
	private String borrowPianist;
	@Persist
	private String borrowWorkman;
	@Persist
	private String borrowSheet;

	// Dvije stranice na koje mozemo da predjemo kada kliknemo na submit dugme
	@InjectPage
	private ViewBorrow viewBorrowPage;
	@InjectPage
	private ViewError viewErrorPage;

	// HashMape za citaoce, bibliotekare, knjige i iznajmljivanja
	@Inject
	private Sheets sheets;
	@Inject
	private Pianists pianists;
	@Inject
	private Workmans workmans;
	@Inject
	private Borrows borrows;

	// Za pregled knjiga
	public Collection<Sheet> getSheets() {
		return sheets.retrieve();
	}

	// Za pregled citalaca
	public Collection<Pianist> getPianists() {
		return pianists.retrieve();
	}

	// Za pregled bibliotekara
	public Collection<Workman> getWorkmans() {
		return workmans.retrieve();
	}

	@OnEvent(value = "submit", component = "borrowInputForm")
	Object onFromSubmit() {
		
		this.borrowWorkman = "WebApp";
		this.borrowPianist = getUser().getUserName();
		
		
		Sheet sheet = null;
		Pianist pianist = null;
		Workman workman = null;

		try {
			pianist = pianists.retrieveSinglePianist(getBorrowPianist());
			workman = workmans
					.retrieveSingleWorkman(getBorrowWorkman());
			sheet = sheets.retrieveSingleSheet(getBorrowSheet());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}

		// Provjera detekcije objekata reader, librarian, book
		if (pianist == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Pianist u Map-i");
			return viewErrorPage;
		}
		if (workman == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Workman u Map-i");
			return viewErrorPage;
		}
		if (sheet == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Sheet u Map-i");
			return viewErrorPage;
		}

		Borrow borrowBean = new BorrowBean();
		borrowBean.executeBorrowCreation(pianist, workman, sheet, borrows);

		this.borrowSheet = "";
		this.borrowWorkman = "";
		this.borrowPianist = "";
		
		return viewBorrowPage;
	}

	//get i set
	public String getBorrowPianist() {
		return borrowPianist;
	}

	public void setBorrowPianist(String borrowPianist) {
		this.borrowPianist = borrowPianist;
	}

	public String getBorrowWorkman() {
		return borrowWorkman;
	}

	public void setBorrowWorkman(String borrowWorkman) {
		this.borrowWorkman = borrowWorkman;
	}

	public String getBorrowSheet() {
		return borrowSheet;
	}

	public void setBorrowSheet(String borrowSheet) {
		this.borrowSheet = borrowSheet;
	}
	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		return null;
	}

}
